//console.log("YMK Goodbye World Title.");
//copyToClipboard("YMK Goodbye World Title.");

key('c', function() { 
        //console.log("YMK press key c");
        detail = document.getElementsByClassName("explanations")[0];
        if (detail != undefined) {
            //console.log("YMK details-panel: " + detail.innerHTML);
            detail_str = getDetailText(detail);
            copyToClipboard(detail_str);
        }
    }
);

/* YMK: Not worked at this time, Wed, 10 Jul 2013 13:26:47 +0800 */
/*removeElementId("ygma");
removeElementId("sponsor");
removeElementId("tabs");
removeElementId("explore-module");
removeElementId("ad-be");
removeElementId("east");
removeElementId("ft");*/

removeElementId("yuhead-hd");
removeElementId("yuhead-bd");
removeElementClass("ysm");
removeElementClass("ydict-course srp");
removeElementClass("yom-ad-lrec mod ");
removeElementClass("yom-ad-linkspot mod");
removeElementClass("y-u-1 ydict-video mod");
removeElementClass("y-u-1 ydict-feature-coursex3 mod");
removeElementClass("y-u-1 ydict-hotarticle mod");
removeElementClass("y-u-1 ydict-tools mod");

function getDetailText(d) {
    var rstr = "";

    /*for (i = 0; i < d.childNodes.length; i++) {
        console.log("Node[" + i + "]: " + d.childNodes[i].nodeName);
    }*/

    /*if (d.childNodes[1] == undefined)
        return null;*/

    //exp = d.childNodes[1];
    exp = d;
    /*for (i = 0; i < exp.childNodes.length; i++)
        console.log("Node[" + i + "]: " + exp.childNodes[i].nodeName);*/

    for (i = 0; i < exp.childNodes.length; i++) {
        typeitem_node = exp.childNodes[i];
        if (typeitem_node.className == "type-item") {
            /* type */
            type_node = typeitem_node.childNodes[1];
            if (type_node == undefined || type_node.className != "type")
                break;
            rstr += type_node.innerHTML + "\n";

            explist_node = typeitem_node.childNodes[3];
            if (explist_node == undefined || explist_node.className != "exp-list")
                break;
            expidx = 0;
            for (j = 0; j < explist_node.childNodes.length; j++) {
                expitem_node = explist_node.childNodes[j];
                if (expitem_node == undefined || expitem_node.className != "exp-item")
                    continue;
                /*for (kk = 0; kk < expitem_node.childNodes.length; kk++)
                    console.log("Node[" + kk + "]: " + expitem_node.childNodes[kk].nodeName);*/
                expidx++
                rstr += "    " + expidx + ". ";
                rstr += expitem_node.childNodes[1].innerText;
                rstr += "\n";
                rr = getDetailExample(expitem_node.childNodes[3]);
                rstr += rr + "\n";
            }
        }
    }

    /*for (i = 0; i < d.childNodes[1].childNodes.length; i++) {
        type_node = d.childNodes[1].childNodes[i];
        if (type_node.className == "def clr nobr") {
            rstr += type_node.childNodes[0].innerHTML + "\n";
            list_node = type_node.childNodes[1];
            if (list_node != undefined) {
                for (j = 0; j < list_node.childNodes[0].childNodes.length; j++) {
                    rstr += "    " + (j+1) + ". ";
                    rstr += list_node.childNodes[0].childNodes[j].childNodes[0].childNodes[0].innerHTML;
                    rstr += "\n";
                    if (list_node.childNodes[0].childNodes[j].childNodes.length > 1) {
                        rstr += "    ";
                        //rstr += list_node.childNodes[0].childNodes[j].childNodes[1].innerHTML;
                        rr = getDetailExample(list_node.childNodes[0].childNodes[j].childNodes[1]);
                        rstr += rr + "\n";
                    }
                    rstr += "\n";
                }
            }
            //console.log("YMK return string: " + rstr);
        }
    }*/
    //console.log("YMK return string: " + rstr);
    return rstr.trim() + "\n";
}

function getDetailExample(d) {
    /* YMK for Wed, 10 Jul 2013 16:48:02 +0800 */
    var rstr = "";
    var i = 0;
    if (d.lastChild == undefined || d.lastChild.nodeValue.trim().length == 0)
        return rstr;
    for (i = 0; i < d.childNodes.length - 1; i++) {
        //console.log("YMK: nodeName " + d.childNodes[i].nodeName);
        if (d.childNodes[i].nodeName == "A" || d.childNodes[i].nodeName == "B") {
            //console.log("YMK:ab [" + d.childNodes[i].innerHTML + "]");
            rstr += d.childNodes[i].innerHTML;
        } else {
            if (d.childNodes[i].nodeValue != null) {
                //console.log("YMK:o [" + d.childNodes[i].nodeValue.trim() + "]");
                rstr += d.childNodes[i].nodeValue.trim() + " ";
            }
        }
    }

    //rstr += "\n"
    //rstr += "        " + d.lastChild.nodeValue.trim();
    rstr += d.lastChild.nodeValue.trim().replace(/ +\n\s+/gi, "\n        ");
    return "        " + rstr.trim() + "\n";
}

function copyToClipboard(p) {
    if (typeof p == "string")
        chrome.extension.sendMessage({ handler: "yclipcopy", data: p }, function(response) {
            //console.log(response.farewell);
        });
}

function removeElementId(p) {
    if (typeof p == "string")
        p = document.getElementById(p);
    if (p != undefined) {
        //console.log("YMK Remove Id: " + p);
        p.parentNode.removeChild(p);
    }
}

function removeElementClass(p) {
    var pclass
    if (typeof p == "string")
        pclass = document.getElementsByClassName(p)[0];
    if (pclass != undefined) {
        //console.log("YMK Remove Class: " + pclass.className);
        pclass.parentNode.removeChild(pclass);
    }
}

/*
vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
*/
